<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org">
<head>
    <base th:href="${#request.getContextPath()}+'/'">
    <link rel="stylesheet" href="static/element/index.css"/>
</head>
<body>
<div id="app">
    <template>
        <div style="margin-bottom:18px;">
            <el-input style="width: 13%" v-model="queryForm.roleName" placeholder="角色名称"></el-input>
            <el-button @click="submitQueryForm">查询</el-button>
            <el-button v-if="button.save" @click="openSaveDialog">新增</el-button>
        </div>

        <el-table :data="tableData" :row-style="{height:'40px'}" :cell-style="{padding:'0px'}" style="font-size: 14px"
                  border>
            <el-table-column type="index" label="序号" width="60"></el-table-column>
            <el-table-column prop="roleName" label="角色名称"></el-table-column>
            <el-table-column prop="remark" label="备注"></el-table-column>
            <el-table-column prop="createTime" label="创建时间"></el-table-column>
            <el-table-column label="操作" width="160">
                <template slot-scope="scope">
                    <el-button v-if="button.update" type="text" @click="openUpdateDialog(scope.row)">修改</el-button>
                    <el-button v-if="button.delete" type="text" @click="del(scope.row)">删除</el-button>
                </template>
            </el-table-column>
        </el-table>
        <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="currentPage"
                       :page-sizes="[10, 50, 100]" :page-size="10" layout="total, sizes, prev, pager, next, jumper"
                       :total="total">
        </el-pagination>

        <el-dialog title="新增" width="70%" :visible.sync="saveDialog" :modal="false" :close-on-click-modal="false">
            <el-form :model="saveForm" ref="saveForm" :rules="rules" label-width="100px">
                <el-row>
                    <el-col :span="10">
                        <el-form-item label="角色名称" prop="roleName">
                            <el-input v-model="saveForm.roleName"></el-input>
                        </el-form-item>
                    </el-col>
                    <el-col :span="10">
                        <el-form-item label="备注" prop="remark">
                            <el-input v-model="saveForm.remark"></el-input>
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-tree :data="menuData" show-checkbox node-key="id" ref="saveTree" :props="defaultProps"
                         prop="menuId"></el-tree>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click="saveDialog = false">取 消</el-button>
                <el-button type="primary" @click="save">确 定</el-button>
            </div>
        </el-dialog>

        <el-dialog title="修改" width="70%" :visible.sync="updateDialog" :modal="false" :close-on-click-modal="false">
            <el-form :model="updateForm" ref="updateForm" :rules="rules" label-width="100px">
                <el-row>
                    <el-col :span="10">
                        <el-form-item label="角色名称" prop="roleName">
                            <el-input v-model="updateForm.roleName"></el-input>
                        </el-form-item>
                    </el-col>
                    <el-col :span="10">
                        <el-form-item label="备注" prop="remark">
                            <el-input v-model="updateForm.remark"></el-input>
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-tree :data="menuData" show-checkbox node-key="id" ref="updateTree" :props="defaultProps"></el-tree>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click="updateDialog = false">取 消</el-button>
                <el-button type="primary" @click="update">确 定</el-button>
            </div>
        </el-dialog>
    </template>
</div>
</body>
<script src="static/js/axios.min.js"></script>
<script src="static/js/api.js"></script>
<script src="static/js/vue.min.js"></script>
<script src="static/element/index.js"></script>
<script>
    new Vue({
        el: '#app',
        data() {
			return {
			    currentPage: 1,
                pageSize: 10,
                total: 0,
                tableData: [],
                saveDialog: false,
                updateDialog: false,
                menuData: [],
                defaultProps: {
					children: 'children',
					label: 'menuName'
				},
				button: {
                    save: false,
                    update: false,
                    delete: false
                },
                saveForm: {
                    roleName: '',
                    remark: '',
                    menuId: []
                },
                updateForm: {
                	id: '',
                    roleName: '',
                    remark: '',
                    menuId: []
                },
                queryForm: {
                    roleName: ''
                },
                rules: {
                    roleName: [
                        {required: true, message: '请输入', trigger: 'blur'},
                        {min: 2, max: 10, message: '长度在 2 到 10 个字符', trigger: 'blur'}
                    ]
                }
			}
        },
        mounted() {
        },
        methods: {
        	handleCurrentChange(val) {
                this.currentPage = val;
                this.submitQueryForm();
			},
			handleSizeChange(val) {
                this.pageSize = val;
                this.submitQueryForm();
            },
            submitQueryForm() {
                let queryParam = {
                    pageNo:this.currentPage,
                    pageSize:this.pageSize,
                    roleName:this.queryForm.roleName
                };
                axios.post('sys/role/list', queryParam)
                .then((res) => {
                    if (res.code == '0000') {
                        this.tableData = res.data.list;
                        this.total = res.data.count;
                    }
                });
            },
            openSaveDialog() {
				this.saveDialog = true;
				if (this.$refs.saveForm !== undefined) {
                    this.$refs.saveForm.resetFields();
                }
				axios.post('sys/menu/list', {})
				.then((res) => {
					if (res.code == '0000') {
						this.menuData = res.data;
					}
				});
            },
            save() {
                this.$refs.saveForm.validate((valid) => {
                    if (valid) {
                        this.saveDialog = false;
                        let saveParam = this.saveForm;
                        saveParam.menuId = this.$refs.saveTree.getCheckedKeys().concat(this.$refs.saveTree.getHalfCheckedKeys());
                        axios.post('sys/role/save', JSON.parse(JSON.stringify(saveParam)))
                        .then((res) => {
                            if (res.code == '0000') {
                                this.submitQueryForm();
                                this.$message({
                                    type: 'success',
                                    message: '新增成功'
                                });
                            } else {
                                this.$message.error(res.message);
                            }
                        });
                    } else {
                        return false;
                    }
                });
            },
            del(row) {
                this.$confirm('您确定删除吗', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                	axios.post('sys/role/delete/' + row.id)
                    .then((res) => {
                        if (res.code == '0000') {
                            this.submitQueryForm();
                            this.$message({
                                type: 'success',
                                message: '删除成功'
                            });
                        } else {
                            this.$message.error(res.message);
                        }
                    });
                }).catch(() => {
                });
            },
            openUpdateDialog(row) {
                this.updateDialog = true;
                axios.post('sys/menu/list', {})
                .then((res) => {
                    if (res.code == '0000') {
                        this.menuData = res.data;
                    }
                });
                axios.get('sys/role/get/' + row.id)
                .then((res) => {
                    if (res.code == '0000') {
                        this.updateForm.id = res.data.id;
						this.updateForm.roleName = res.data.roleName;
						this.updateForm.remark = res.data.remark;
						if (res.data.menuId != null) {
							this.$refs.updateTree.setCheckedKeys(res.data.menuId);
						}
                    }
                });
            },
            update() {
                this.$refs.updateForm.validate((valid) => {
                    if (valid) {
                        this.updateDialog = false;
                        let updateParam = this.updateForm;
                        updateParam.menuId = this.$refs.updateTree.getCheckedKeys().concat(this.$refs.updateTree.getHalfCheckedKeys());;
                        axios.post('sys/role/update', JSON.parse(JSON.stringify(updateParam)))
                        .then((res) => {
                            if (res.code == '0000') {
                                this.submitQueryForm();
                                this.$message({
                                    type: 'success',
                                    message: '修改成功'
                                });
                            } else {
                                this.$message.error(res.message);
                            }
                        });
                    } else {
                        return false;
                    }
                });
            }
        },
        created() {
			this.submitQueryForm();
			let sysButtons = JSON.parse(localStorage.getItem("jsbb-sysButtons"));
			this.button.save = sysButtons.indexOf("sys/role/save") > -1;
			this.button.update = sysButtons.indexOf("sys/role/update") > -1;
			this.button.delete = sysButtons.indexOf("sys/role/delete") > -1;
        }
    });
</script>
</html>
